home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / manage / snmp / cmu-snmp1.0 / apps / snmpnetstat / if.c next >
Encoding:
C/C++ Source or Header  |  1989-09-20  |  9.5 KB  |  342 lines

  1. /***********************************************************
  2.     Copyright 1989 by Carnegie Mellon University
  3.  
  4.                       All Rights Reserved
  5.  
  6. Permission to use, copy, modify, and distribute this software and its 
  7. documentation for any purpose and without fee is hereby granted, 
  8. provided that the above copyright notice appear in all copies and that
  9. both that copyright notice and this permission notice appear in 
  10. supporting documentation, and that the name of CMU not be
  11. used in advertising or publicity pertaining to distribution of the
  12. software without specific, written prior permission.  
  13.  
  14. CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  15. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  16. CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  17. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  18. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  19. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  20. SOFTWARE.
  21. ******************************************************************/
  22. /*
  23.  * Copyright (c) 1983,1988 Regents of the University of California.
  24.  * All rights reserved.
  25.  *
  26.  * Redistribution and use in source and binary forms are permitted
  27.  * provided that this notice is preserved and that due credit is given
  28.  * to the University of California at Berkeley. The name of the University
  29.  * may not be used to endorse or promote products derived from this
  30.  * software without specific prior written permission. This software
  31.  * is provided ``as is'' without express or implied warranty.
  32.  */
  33.  
  34. #include <sys/types.h>
  35. #include <sys/socket.h>
  36.  
  37. #include <netinet/in.h>
  38.  
  39. #include <stdio.h>
  40. #include <signal.h>
  41.  
  42. #include "main.h"
  43. #include "asn1.h"
  44. #include "snmp.h"
  45. #include "snmp_impl.h"
  46. #include "snmp_api.h"
  47. #include "snmp_client.h"
  48. #include "mib.h"
  49.  
  50. #define    YES    1
  51. #define    NO    0
  52.  
  53. extern    int nflag;
  54. extern    char *interface;
  55. extern    char *routename(), *netname();
  56. extern    struct snmp_session *Session;
  57. extern    struct variable_list *getvarbyname();
  58.  
  59. oid oid_ifname[] = {1, 3, 6, 1, 2, 1, 2, 2, 1, 2, 1};
  60. static oid oid_ifinucastpkts[] = {1, 3, 6, 1, 2, 1, 2, 2, 1, 11, 1};
  61. static oid oid_cfg_nnets[] = {1, 3, 6, 1, 2, 1, 2, 1, 0};
  62.  
  63. #define IFNAME        2
  64. #define IFMTU        4
  65. #define IFOPERSTATUS    8
  66. #define INUCASTPKTS    11
  67. #define INNUCASTPKTS    12
  68. #define INERRORS    14
  69. #define OUTUCASTPKTS    17
  70. #define OUTNUCASTPKTS    18
  71. #define OUTERRORS    20
  72.  
  73. /*
  74.  * Print a description of the network interfaces.
  75.  */
  76. intpr(interval)
  77.     int interval;
  78. {
  79.     oid varname[MAX_NAME_LEN], *instance, *ifentry;
  80.     int varname_len;
  81.     int ifnum, cfg_nnets;
  82.     struct variable_list *var;
  83.     char name[32];
  84.     int mtu;
  85.     int ipkts, ierrs, opkts, oerrs, operstatus, collisions;
  86.  
  87.     if (interval) {
  88.         sidewaysintpr((unsigned)interval);
  89.         return;
  90.     }
  91.     printf("%-11.11s %-5.5s %-11.11s %-15.15s %8.8s %5.5s %8.8s %5.5s",
  92.         "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs",
  93.         "Opkts", "Oerrs");
  94.     putchar('\n');
  95.     var = getvarbyname(Session, oid_cfg_nnets, sizeof(oid_cfg_nnets) / sizeof(oid));
  96.     if (var)
  97.         cfg_nnets = *var->val.integer;
  98.     else
  99.         return;
  100.     bcopy((char *)oid_ifname, (char *)varname, sizeof(oid_ifname));
  101.     varname_len = sizeof(oid_ifname) / sizeof(oid);
  102.     ifentry = varname + 9;
  103.     instance = varname + 10;
  104.     for (ifnum = 1; ifnum <= cfg_nnets; ifnum++) {
  105.         register char *cp;
  106.         char *index();
  107.  
  108.         *name = mtu = 0;
  109.         ipkts = ierrs = opkts = oerrs = operstatus = collisions = 0;
  110.         *instance = ifnum;
  111.         *ifentry = IFNAME;
  112.         var = getvarbyname(Session, varname, varname_len);
  113.         if (var){
  114.             bcopy((char *)var->val.string, name, var->val_len);
  115.             name[var->val_len] = 0;
  116.         }
  117.         *ifentry = IFMTU;
  118.         var = getvarbyname(Session, varname, varname_len);
  119.         if (var)
  120.             mtu = *var->val.integer;
  121.         *ifentry = IFOPERSTATUS;
  122.         var = getvarbyname(Session, varname, varname_len);
  123.         if (var)
  124.             operstatus = *var->val.integer;
  125.         *ifentry = INUCASTPKTS;
  126.         var = getvarbyname(Session, varname, varname_len);
  127.         if (var)
  128.             ipkts = *var->val.integer;
  129.         *ifentry = INNUCASTPKTS;
  130.         var = getvarbyname(Session, varname, varname_len);
  131.         if (var)
  132.             ipkts += *var->val.integer;
  133.         *ifentry = INERRORS;
  134.         var = getvarbyname(Session, varname, varname_len);
  135.         if (var)
  136.             ierrs = *var->val.integer;
  137.         *ifentry = OUTUCASTPKTS;
  138.         var = getvarbyname(Session, varname, varname_len);
  139.         if (var)
  140.             opkts = *var->val.integer;
  141.         *ifentry = OUTNUCASTPKTS;
  142.         var = getvarbyname(Session, varname, varname_len);
  143.         if (var)
  144.             opkts += *var->val.integer;
  145.         *ifentry = OUTERRORS;
  146.         var = getvarbyname(Session, varname, varname_len);
  147.         if (var)
  148.             oerrs = *var->val.integer;
  149.  
  150.         name[15] = '\0';
  151.         if (interface != 0 &&
  152.             strcmp(name, interface) != 0)
  153.             continue;
  154.         cp = index(name, '\0');
  155.         if (operstatus != MIB_IFSTATUS_UP)
  156.             *cp++ = '*';
  157.         *cp = '\0';
  158.         printf("%-11.11s %-5d ", name, mtu);
  159.         printf("%-11.11s ", "none");
  160.         printf("%-15.15s ", "none");
  161.         printf("%8d %5d %8d %5d %5d",
  162.             ipkts, ierrs,
  163.             opkts, oerrs, collisions);
  164.         putchar('\n');
  165.     }
  166. }
  167.  
  168. #define    MAXIF    10
  169. struct    iftot {
  170.     char    ift_name[16];        /* interface name */
  171.     int    ift_ip;            /* input packets */
  172.     int    ift_ie;            /* input errors */
  173.     int    ift_op;            /* output packets */
  174.     int    ift_oe;            /* output errors */
  175.     int    ift_co;            /* collisions */
  176. } iftot[MAXIF];
  177.  
  178. u_char    signalled;            /* set if alarm goes off "early" */
  179.  
  180. /*
  181.  * Print a running summary of interface statistics.
  182.  * Repeat display every interval seconds, showing statistics
  183.  * collected over that interval.  Assumes that interval is non-zero.
  184.  * First line printed at top of screen is always cumulative.
  185.  */
  186. sidewaysintpr(interval)
  187.     unsigned interval;
  188. {
  189.     register struct iftot *ip, *total;
  190.     register int line;
  191.     struct iftot *lastif, *sum, *interesting, ifnow, *now = &ifnow;
  192.     int oldmask;
  193.     int catchalarm();
  194.     struct variable_list *var;
  195.     oid varname[MAX_NAME_LEN], *instance, *ifentry;
  196.     int varname_len;
  197.     int ifnum, cfg_nnets;
  198.     char *index();
  199.  
  200.     lastif = iftot;
  201.     sum = iftot + MAXIF - 1;
  202.     total = sum - 1;
  203.     interesting = iftot;
  204.     var = getvarbyname(Session, oid_cfg_nnets, sizeof(oid_cfg_nnets) / sizeof(oid));
  205.     if (var)
  206.         cfg_nnets = *var->val.integer;
  207.     else
  208.         return;
  209.     bcopy((char *)oid_ifname, (char *)varname, sizeof(oid_ifname));
  210.     varname_len = sizeof(oid_ifname) / sizeof(oid);
  211.     for (ifnum = 1, ip = iftot; ifnum <= cfg_nnets; ifnum++) {
  212.         char *cp;
  213.  
  214.         ip->ift_name[0] = '(';
  215.         varname[10] = ifnum;
  216.         var = getvarbyname(Session, varname, varname_len);
  217.         if (var){
  218.             bcopy((char *)var->val.string, ip->ift_name + 1, var->val_len);
  219.         }
  220.         if (interface && strcmp(ip->ift_name + 1, interface) == 0)
  221.             interesting = ip;
  222.         ip->ift_name[15] = '\0';
  223.         cp = index(ip->ift_name, '\0');
  224.         sprintf(cp, ")");
  225.         ip++;
  226.         if (ip >= iftot + MAXIF - 2)
  227.             break;
  228.     }
  229.     lastif = ip;
  230.  
  231.     (void)signal(SIGALRM, catchalarm);
  232.     signalled = NO;
  233.     (void)alarm(interval);
  234. banner:
  235.     printf("    input   %-6.6s    output       ", interesting->ift_name);
  236.     if (lastif - iftot > 0)
  237.         printf("     input  (Total)    output");
  238.     for (ip = iftot; ip < iftot + MAXIF; ip++) {
  239.         ip->ift_ip = 0;
  240.         ip->ift_ie = 0;
  241.         ip->ift_op = 0;
  242.         ip->ift_oe = 0;
  243.         ip->ift_co = 0;
  244.     }
  245.     putchar('\n');
  246.     printf("%8.8s %5.5s %8.8s %5.5s %5.5s ",
  247.         "packets", "errs", "packets", "errs", "colls");
  248.     if (lastif - iftot > 0)
  249.         printf("%8.8s %5.5s %8.8s %5.5s %5.5s ",
  250.             "packets", "errs", "packets", "errs", "colls");
  251.     putchar('\n');
  252.     fflush(stdout);
  253.     line = 0;
  254. loop:
  255.     sum->ift_ip = 0;
  256.     sum->ift_ie = 0;
  257.     sum->ift_op = 0;
  258.     sum->ift_oe = 0;
  259.     sum->ift_co = 0;
  260.     bcopy((char *)oid_ifinucastpkts, (char *)varname, sizeof(oid_ifinucastpkts));
  261.     varname_len = sizeof(oid_ifinucastpkts) / sizeof(oid);
  262.     ifentry = varname + 9;
  263.     instance = varname + 10;
  264.     for (ifnum = 1, ip = iftot; ifnum <= cfg_nnets && ip < lastif; ip++, ifnum++) {
  265.         bzero((char *)now, sizeof(*now));
  266.         *instance = ifnum;
  267.         *ifentry = INUCASTPKTS;
  268.         var = getvarbyname(Session, varname, varname_len);
  269.         if (var)
  270.             now->ift_ip = *var->val.integer;
  271.         *ifentry = INNUCASTPKTS;
  272.         var = getvarbyname(Session, varname, varname_len);
  273.         if (var)
  274.             now->ift_ip += *var->val.integer;
  275.         *ifentry = INERRORS;
  276.         var = getvarbyname(Session, varname, varname_len);
  277.         if (var)
  278.             now->ift_ie = *var->val.integer;
  279.         *ifentry = OUTUCASTPKTS;
  280.         var = getvarbyname(Session, varname, varname_len);
  281.         if (var)
  282.             now->ift_op = *var->val.integer;
  283.         *ifentry = OUTNUCASTPKTS;
  284.         var = getvarbyname(Session, varname, varname_len);
  285.         if (var)
  286.             now->ift_op += *var->val.integer;
  287.         *ifentry = OUTERRORS;
  288.         var = getvarbyname(Session, varname, varname_len);
  289.         if (var)
  290.             now->ift_oe = *var->val.integer;
  291.  
  292.         if (ip == interesting)
  293.             printf("%8d %5d %8d %5d %5d ",
  294.                 now->ift_ip - ip->ift_ip,
  295.                 now->ift_ie - ip->ift_ie,
  296.                 now->ift_op - ip->ift_op,
  297.                 now->ift_oe - ip->ift_oe,
  298.                 now->ift_co - ip->ift_co);
  299.         ip->ift_ip = now->ift_ip;
  300.         ip->ift_ie = now->ift_ie;
  301.         ip->ift_op = now->ift_op;
  302.         ip->ift_oe = now->ift_oe;
  303.         ip->ift_co = now->ift_co;
  304.         sum->ift_ip += ip->ift_ip;
  305.         sum->ift_ie += ip->ift_ie;
  306.         sum->ift_op += ip->ift_op;
  307.         sum->ift_oe += ip->ift_oe;
  308.         sum->ift_co += ip->ift_co;
  309.     }
  310.     if (lastif - iftot > 0)
  311.         printf("%8d %5d %8d %5d %5d ",
  312.             sum->ift_ip - total->ift_ip,
  313.             sum->ift_ie - total->ift_ie,
  314.             sum->ift_op - total->ift_op,
  315.             sum->ift_oe - total->ift_oe,
  316.             sum->ift_co - total->ift_co);
  317.     *total = *sum;
  318.     putchar('\n');
  319.     fflush(stdout);
  320.     line++;
  321.     oldmask = sigblock(sigmask(SIGALRM));
  322.     if (! signalled) {
  323.         sigpause(0);
  324.     }
  325.     sigsetmask(oldmask);
  326.     signalled = NO;
  327.     (void)alarm(interval);
  328.     if (line == 21)
  329.         goto banner;
  330.     goto loop;
  331.     /*NOTREACHED*/
  332. }
  333.  
  334. /*
  335.  * Called if an interval expires before sidewaysintpr has completed a loop.
  336.  * Sets a flag to not wait for the alarm.
  337.  */
  338. catchalarm()
  339. {
  340.     signalled = YES;
  341. }
  342.